<template>
<el-dialog title="详情" :visible.sync="detailDialogShow" width="1000px" append-to-body>
  <div class="app-container">
    <!-- <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单ID" prop="orderId">
        <el-input
          v-model="queryParams.orderId"
          placeholder="请输入订单ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form> -->


    <el-table v-loading="loading" :data="detailsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单编号" align="center" prop="orderNum" />
      <!-- <el-table-column label="订单状态" align="center" prop="ticketName" /> -->
      <el-table-column label="门票名称" align="center" prop="ticketName" />
      <el-table-column label="数量" align="center" prop="ticketCount" />
      <el-table-column label="物流单号" align="center" prop="trackingCode" />
      <!-- <el-table-column label="游客姓名" align="center" prop="orderId" />
      <el-table-column label="游客手机" align="center" prop="orderNum" />
      <el-table-column label="游客身份证号" align="center" prop="ticketTypeId" /> -->
      <!-- <el-table-column label="下单日期" align="center" prop="ticketMoney" /> -->
      <!-- <el-table-column label="可用时间开始" align="center" prop="ticketUsetimeStart" width="180">
      </el-table-column>
      <el-table-column label="可用时间结束" align="center" prop="ticketUsetimeEnd" width="180">
      </el-table-column> -->
      <el-table-column label="状态" align="center" prop="ticketStatus" >
          <template slot-scope="scope">
            <dict-tag :options="dict.type.order_status" :value="scope.row.ticketStatus"/>
          </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" min-width="130">
        <template slot-scope="scope">
        <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleOrderNo(scope.row)"
          >回填物流单号</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <!-- <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    /> -->
    <!-- 回填物流单号 -->
    <el-dialog title="回填物流单号" :visible.sync="orderNoDialogVisible" width="500px" append-to-body :close-on-click-modal="dialogIsCloseOnModal" :close-on-press-escape="dialogIsCloseOnModal">
      <el-form ref="formOrder" :model="formOrder" :rules="rulesOrder" label-width="80px">
        <el-form-item label="物流单号" prop="orderNo">
          <el-input v-model="formOrder.orderNo" placeholder="请输入物流单号" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitOrderNo">确 定</el-button>
        <el-button @click="cancelOrder">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改订单详情对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="门票/商品id" prop="ticketId">
          <el-input v-model="form.ticketId" placeholder="请输入门票/商品id" />
        </el-form-item>
        <el-form-item label="门票/商品名称" prop="ticketName">
          <el-input v-model="form.ticketName" placeholder="请输入门票/商品名称" />
        </el-form-item>
        <el-form-item label="订单详情号" prop="orderDetailsNum">
          <el-input v-model="form.orderDetailsNum" placeholder="请输入订单详情号" />
        </el-form-item>
        <el-form-item label="订单id" prop="orderId">
          <el-input v-model="form.orderId" placeholder="请输入订单id" />
        </el-form-item>
        <el-form-item label="订单code" prop="orderNum">
          <el-input v-model="form.orderNum" placeholder="请输入订单code" />
        </el-form-item>
        <el-form-item label="门票票种id" prop="ticketTypeId">
          <el-input v-model="form.ticketTypeId" placeholder="请输入门票票种id" />
        </el-form-item>
        <el-form-item label="金额" prop="ticketMoney">
          <el-input v-model="form.ticketMoney" placeholder="请输入金额" />
        </el-form-item>
        <el-form-item label="数量" prop="ticketCount">
          <el-input v-model="form.ticketCount" placeholder="请输入数量" />
        </el-form-item>
        <el-form-item label="门票配置表id" prop="ticConfigId">
          <el-input v-model="form.ticConfigId" placeholder="请输入门票配置表id" />
        </el-form-item>
        <el-form-item label="可用时间开始" prop="ticketUsetimeStart">
          <el-date-picker clearable
            v-model="form.ticketUsetimeStart"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择可用时间开始">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="可用时间结束" prop="ticketUsetimeEnd">
          <el-date-picker clearable
            v-model="form.ticketUsetimeEnd"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择可用时间结束">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="门票/商品类型名字" prop="ticketTypeName">
          <el-input v-model="form.ticketTypeName" placeholder="请输入门票/商品类型名字" />
        </el-form-item>
        <el-form-item label="商品介绍" prop="shopDetails">
          <el-input v-model="form.shopDetails" placeholder="请输入商品介绍" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
  <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="detailsubmitForm">确 定</el-button>
        <el-button @click="detailcancel">取 消</el-button>
      </div>
</el-dialog>
</template>

<script>
import { listDetails, getDetails, delDetails, addDetails, updateDetails,backfill } from "@/api/order/details";

export default {
  name: "Details",
  dicts:['order_status'],
  data() {
    return {
      // 详情弹框
      detailDialogShow:false,
      orderNoDialogVisible:false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单详情表格数据
      detailsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        ticketId: null,
        ticketName: null,
        orderTicketType: null,
        orderDetailsNum: null,
        orderId: null,
        orderNum: null,
        ticketTypeId: null,
        ticketMoney: null,
        ticketCount: null,
        ticConfigId: null,
        ticketUsetimeStart: null,
        ticketUsetimeEnd: null,
        ticketStatus: null,
        ticketTypeName: null,
        shopDetails: null
      },
      // 表单参数
      form: {},
      formOrder:{
        orderNo:'',
        orderDetailId:''
      },
      // 表单校验
      rules: {
      },
      rulesOrder:{
        orderNo:[
           { required: true, trigger: "blur", message: "请输入物流单号" },
        ]
      },
      orderId:''
    };
  },
  created() {
    // 弹出新页面形式
    // const orderId = this.$route.params && this.$route.params.orderId;
    // this.orderId=orderId
    // console.log(orderId)
    // this.getList(orderId);
    
  },
  methods: {
    isOpen(orderId){
      // 弹框形式
      this.getList(orderId);
      this.detailDialogShow=true
      
    },
    detailsubmitForm(){
      this.reset()
      this.detailDialogShow=false
    },
    detailcancel(){
      this.reset()
      this.detailDialogShow=false
    },
    handleOrderNo(row){
      this.orderNoDialogVisible=true
      this.formOrder.orderDetailId=row.orderDetailsId
      this.formOrder.orderNo=row.trackingCode || ''
    },
    submitOrderNo(){
      this.$refs["formOrder"].validate(valid => {
        if (valid) {
          let params={
            orderDetailId:this.formOrder.orderDetailId,
            trackingCode:this.formOrder.orderNo
          }
          console.log(params)
          backfill(params).then(res=>{
            this.getList()
            this.cancelOrder()
          })
        }
      })
    },
    cancelOrder(){
      this.orderNoDialogVisible=false
      this.formOrder.orderNo=''
      this.resetForm("formOrder");
    },
    /** 查询订单详情列表 */
    getList(orderId) {
      this.loading = true;
      let temp=orderId || this.orderId
      listDetails(temp).then(response => {
        this.detailsList = response.data;
        // this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        orderDetailsId: null,
        ticketId: null,
        ticketName: null,
        orderTicketType: null,
        orderDetailsNum: null,
        orderId: null,
        orderNum: null,
        ticketTypeId: null,
        ticketMoney: null,
        ticketCount: null,
        ticConfigId: null,
        ticketUsetimeStart: null,
        ticketUsetimeEnd: null,
        ticketStatus: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        isDel: null,
        ticketTypeName: null,
        shopDetails: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.orderDetailsId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单详情";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const orderDetailsId = row.orderDetailsId || this.ids
      getDetails(orderDetailsId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改订单详情";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.orderDetailsId != null) {
            updateDetails(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDetails(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const orderDetailsIds = row.orderDetailsId || this.ids;
      this.$modal.confirm('是否确认删除订单详情编号为"' + orderDetailsIds + '"的数据项？').then(function() {
        return delDetails(orderDetailsIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('order/details/export', {
        ...this.queryParams
      }, `details_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
